package com.study.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.app.domain.UserProfile;
import com.study.app.mapper.UserProfileMapper;
import com.study.app.service.IUserProfileService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户信息扩展Service业务层处理
 *
 * @author study
 * @date 2023-04-19
 */
@Service
public class UserProfileServiceImpl extends ServiceImpl<UserProfileMapper, UserProfile> implements IUserProfileService {

    @Override
    public List<UserProfile> selectUserProfileList(UserProfile userProfile) {
        QueryWrapper<UserProfile> queryWrapper = new QueryWrapper<>();
        // 添加查询条件
        if (userProfile.getUserId() != null) {
            queryWrapper.eq("user_id", userProfile.getUserId());
        }
        return this.list(queryWrapper);
    }

    @Override
    public UserProfile selectUserProfileByUserId(Long userId) {
        QueryWrapper<UserProfile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.getOne(queryWrapper);
    }

    @Override
    public int incrementUserPosts(Long userId) {
        UserProfile profile = this.selectUserProfileByUserId(userId);
        if (profile != null) {
            profile.setPostsCount(profile.getPostsCount() + 1);
            this.updateById(profile);
            return profile.getPostsCount();
        }
        return 0;
    }

    @Override
    public int decrementUserPosts(Long userId) {
        UserProfile profile = this.selectUserProfileByUserId(userId);
        if (profile != null && profile.getPostsCount() > 0) {
            profile.setPostsCount(profile.getPostsCount() - 1);
            this.updateById(profile);
            return profile.getPostsCount();
        }
        return 0;
    }

    @Override
    public int incrementUserLikes(Long userId) {
        UserProfile profile = this.selectUserProfileByUserId(userId);
        if (profile != null) {
            profile.setLikeCount(profile.getLikeCount() + 1);
            this.updateById(profile);
            return profile.getLikeCount();
        }
        return 0;
    }

    @Override
    public int decrementUserLikes(Long userId) {
        UserProfile profile = this.selectUserProfileByUserId(userId);
        if (profile != null && profile.getLikeCount() > 0) {
            profile.setLikeCount(profile.getLikeCount() - 1);
            this.updateById(profile);
            return profile.getLikeCount();
        }
        return 0;
    }
}